<template>
  <div class="p-2">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="编码" prop="supplierCode">
              <el-input v-model="queryParams.supplierCode" placeholder="请输入供应商编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="名称" prop="supplierName">
              <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>

    <el-card shadow="never">
      <el-table ref = "selectSupplierTable" v-loading="loading" :data="supplierList" >
        <el-table-column label="主键" align="center" prop="supplierId" v-if="false" />
        <el-table-column label="供应商编码" align="center" prop="supplierCode" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="供应商名称" align="center" prop="supplierName" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="供货名称" align="center" prop="supplyType" />
        <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="联系方式" align="center" prop="contact" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="税号" align="center" prop="taxNumber" />
        <el-table-column label="开户行" align="center" prop="depositBank" />
        <el-table-column label="账号" align="center" prop="bankAccount" />
        <el-table-column label="是否合格" align="center" prop="isQualified">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.isQualified"/>
          </template>
        </el-table-column>
        <el-table-column label="城市" align="center" prop="city" />
        <el-table-column label="邮政编码" align="center" prop="postalCode" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button size="mini" type="text" icon="Select" @click="handleSelect(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="SeleteSupplier" lang="ts">
import { listSupplier } from '@/api/wmscloud/supplier';
import { SupplierVO, SupplierQuery } from '@/api/wmscloud/supplier/types';
import { ElTable } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_yes_no } = toRefs<any>(proxy?.useDict('sys_yes_no'));

const supplierList = ref<SupplierVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const supplierFormRef = ref<ElFormInstance>();
const selectSupplierTable = ref<InstanceType<typeof ElTable>>();


const queryParams = reactive<SupplierQuery>({
  pageNum: 1,
  pageSize: 10,
  supplierCode: undefined,
  supplierName: undefined,
  supplyType: undefined,
  address: undefined,
  contact: undefined,
  taxNumber: undefined,
  depositBank: undefined,
  bankAccount: undefined,
  isQualified: undefined,
  city: undefined,
  postalCode: undefined,
  params: {
  }
});

/** 查询供应商列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSupplier(queryParams);
  supplierList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

// 确认选择的方法
const emit = defineEmits<{
  (e: 'confirmSelectSupplier', row: SupplierVO): void
}>();

const handleSelect = (row: SupplierVO) => {
  // 清空当前表格选中项
  selectSupplierTable.value?.clearSelection?.();
  console.log(row)
  // 通过 emit 通知父组件
  emit('confirmSelectSupplier', row);
};


onMounted(() => {
  getList();
});
</script>
